package com.disc.baidu;
import org.apache.http.NameValuePair;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
public class BaseClass {

    public static final String CHARSET = "utf-8";

    public BaiDuLogin baiDuLogin;


    /**
     * List<NameValuePair> 转换成url
     * @param nvps
     * @return
     */
    public static String getEncodedUrl(List<NameValuePair> nvps)  throws UnsupportedEncodingException{
        if (nvps == null || nvps.size()==0)
        {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (NameValuePair valuePair : nvps)
        {
            sb.append(valuePair.getName() + "=" + URLEncoder.encode(String.valueOf((valuePair.getValue() == null ? "" : valuePair.getValue())), CHARSET));
            sb.append("&");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    /**
     * url 复制
     * @param url
     * @param key
     * @param value
     * @return
     */
    public String initUrl(String url,String key,String value)
    {
        String regex = "\\{" +  key +"\\}";

        return url.replaceAll(regex,value);
    }
}
